package com.woniu.zsd.shopping.controller;


import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.common.web.PageInfo;
import com.woniu.zsd.shopping.dto.SelectAllProductDto;
import com.woniu.zsd.shopping.dto.SelectCollectProductDto;
import com.woniu.zsd.shopping.form.AddProductForm;
import com.woniu.zsd.shopping.form.PageForm;
import com.woniu.zsd.shopping.form.SelectProductFrom;
import com.woniu.zsd.shopping.param.AddProductParam;
import com.woniu.zsd.shopping.param.PageParam;
import com.woniu.zsd.shopping.param.SelectProductParam;
import com.woniu.zsd.shopping.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品管理类接口")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private JWTUtil jwtUtil;

    /**
     * 添加商品
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/addProduct")
    @ApiOperation("添加商品")
    public Result addProduct(@RequestBody AddProductForm form) throws Exception {
        productService.addProduct(BeanUtil.toBean(form, AddProductParam.class));
        return Result.success("添加商品成功");
    }

    //删除商品
    @PostMapping("/deleteProduct")
    @ApiOperation("删除商品")
    public Result deleteProduct(Integer productId) throws Exception {
        productService.deleteProduct(productId);
        return Result.success("删除商品成功");
    }

    //查询所有商品
    @GetMapping("/selectAllProduct")
    @ApiOperation("查询商品")
    public Result selectAllProduct(SelectProductFrom form) throws Exception  {
        PageInfo pageInfo = productService.selectAllProduct(BeanUtil.toBean(form, SelectProductParam.class));
        return Result.success("查询所有商品成功",pageInfo);
    }

    //增加商品库存
    @PostMapping("/addProductStock")
    @ApiOperation("添加商品库存")
    public Result addProductStock(Integer productId, Integer stockNum) throws Exception {
        productService.addProductStock(productId, stockNum);
        return Result.success("增加商品库存成功");
    }
    //修改商品积分
    @PostMapping("/updateProductPoint")
    @ApiOperation("修改商品积分")
    public Result updateProductPoint(Integer productId, Integer point) throws Exception {
        productService.updateProductPoint(productId, point);
        return Result.success("修改商品积分成功");
    }
    //修改商品状态(上架下架)
    @PostMapping("/updateProductStatus")
    @ApiOperation("修改商品状态")
    public Result updateProductStatus(Integer productId, String status) throws Exception {
        productService.updateProductStatus(productId, status);
        return Result.success("修改商品状态成功");
    }

    //收藏商品
    @PostMapping("/collectProduct")
    @ApiOperation("收藏商品")
    public Result collectProduct(Integer productId,@RequestHeader("token") String token) throws Exception {
        Integer userId = jwtUtil.getUserId(token);
        productService.collectProduct(productId, userId);
        return Result.success("收藏商品成功");
    }

    //取消收藏商品
    @PostMapping("/cancelCollectProduct")
    @ApiOperation("取消收藏商品")
    public Result cancelCollectProduct(Integer productId,@RequestHeader("token") String token) throws Exception {
        Integer userId = jwtUtil.getUserId(token);
        productService.cancelCollectProduct(productId, userId);
        return Result.success("取消收藏商品成功");
    }

    //分页查询收藏商品
    @GetMapping("/selectCollectProduct")
    @ApiOperation("分页查询收藏商品")
    public Result selectCollectProduct(PageForm form,@RequestHeader("token") String token) throws Exception {
        Integer userId = jwtUtil.getUserId(token);
        PageInfo pageInfo = productService.selectCollectProduct(BeanUtil.toBean(form, PageParam.class),userId);
        return Result.success("查询收藏商品成功",pageInfo);
    }

    //查询排行榜前十的书籍
    @GetMapping("/selectTopTenProduct")
    @ApiOperation("查询排行榜前十的商品")
    public Result selectTopTenProduct() throws Exception {
     List<SelectCollectProductDto> dtos= productService.selectTopTenProduct();
        return Result.success("查询收藏商品成功",dtos);
    }

}
